package com.irdstudio.apicenter.console.controller.appframe;

import java.io.IOException;
import java.io.PrintWriter;
import java.sql.SQLException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.alibaba.fastjson.JSON;
import com.irdstudio.apicenter.console.dm.AppFrameStart;
import com.irdstudio.apicenter.console.service.appframe.AppFrameModuleService;
import com.irdstudio.apicenter.console.service.appframe.AppFrameStartService;

import javax.servlet.http.HttpServletRequest;

/**
 * 平台模块管理
 * 
 * @author jr
 * 
 */
@Controller
@RequestMapping(value = "/appFrameStart")
public class AppFrameStartAction1 {
	@Autowired
	private AppFrameStartService appFrameStartService;
	@Autowired
	private AppFrameModuleService appFrameModuleService;

	@RequestMapping(value = "appFrameStartDetail1")
	public ModelAndView queryWithKeys(HttpServletRequest request)
			throws Exception {
		String appid = request.getParameter("appid");
		ModelAndView modelAndView = new ModelAndView();
		List<HashMap<String, String>> ModuleList = appFrameModuleService
				.queryByAppid(appid);
		AppFrameStart appFrameStart = appFrameStartService
				.queryAppFrameStartDetail(appid);
		// 防止应用查询设置信息时为无的情况，返回appid
		appFrameStart.setAppId(appid);
		modelAndView.addObject("appFrameStart", appFrameStart);
		modelAndView.addObject("ModuleList", ModuleList);
		modelAndView.setViewName("appframe/start");
		return modelAndView;
	}

	@RequestMapping(value = "updateAppFrameStart")
	public void updateAppFrameStart(
			@ModelAttribute("appFrameStart") AppFrameStart appFrameStart,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		// AppFrameStart
		// appFrameStart=(AppFrameStart)request.getAttribute("appFrameStart");
		System.out.println("111#######:" + appFrameStart.getAppCopyright());
		ModelAndView modelAndView = new ModelAndView();
		int flag = -1;
		AppFrameStart appFrameStart1 = appFrameStartService
				.queryAppFrameStartDetail(appFrameStart.getAppId());
		if (appFrameStart1.getAppId() == null
				|| "".endsWith(appFrameStart1.getAppId())) {
			flag = appFrameStartService.insertOne(appFrameStart);
		} else {
			flag = appFrameStartService.updateByKeys(appFrameStart);
		}

		// int flag = appFrameStartService.updateByKeys(appFrameStart);
		/*
		 * modelAndView.addObject("appFrameStart", appFrameStart);
		 * modelAndView.addObject("result",flag);
		 * modelAndView.setViewName("appframe/start"); return modelAndView;
		 */

		Map<String, Object> map = new HashMap<String, Object>(1);
		map.put("flag", flag);
		String jsonResult = JSON.toJSONString(map);
		PrintWriter printWriter = null;
		try {
			printWriter = response.getWriter();
			printWriter.print(jsonResult);
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} finally {
			if (null != printWriter) {
				printWriter.flush();
				printWriter.close();
			}
		}
	}

	/*
	 * 查询模块详细信息 输出json
	 */
	@RequestMapping(value = "appFrameStartDetail")
	public @ResponseBody
	AppFrameStart queryAppFrameStartDetail(Model model,
			@PathVariable("id") String appid) throws SQLException,
			ClassNotFoundException {
		AppFrameStart appFrameStart = appFrameStartService
				.queryAppFrameStartDetail("1");
		return appFrameStart;
	}

	/**
	 * 转到应用设置页面
	 * 
	 * @param request
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value = "appSettingIndex")
	public String appSettingIndex(HttpServletRequest request) throws Exception {

		// 取得传入应用标识
		String appId = request.getParameter("appid");
		if (StringUtils.isEmpty(appId)) {
			throw new Exception("必须传入应用标识(appid)参数!");
		}

		request.setAttribute("appid", appId);

		return "make/appSettingIndex";
	}

	/**
	 * 转到应用配置管理
	 * @param request
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value = "appConfigIndex")
	public String appConfigIndex(HttpServletRequest request) throws Exception {

		// 取得传入应用标识
		String appId = request.getParameter("appid");
		if (StringUtils.isEmpty(appId)) {
			throw new Exception("必须传入应用标识(appid)参数!");
		}

		request.setAttribute("appid", appId);

		return "make/appConfigIndex";
	}
	
	/**
	 * 转到微信参数配置管理 
	 * @param request
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value = "wxConfigIndex")
	public String wxConfigIndex(HttpServletRequest request) throws Exception {

		// 取得传入应用标识
		String appId = request.getParameter("appid");
		if (StringUtils.isEmpty(appId)) {
			throw new Exception("必须传入应用标识(appid)参数!");
		}

		request.setAttribute("appid", appId);

		return "make/wxConfigIndex";
	}	
	

	/**
	 * 转到应用编译打包
	 * 
	 * @param request
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value = "appCompileIndex")
	public String appCompileIndex(HttpServletRequest request) throws Exception {

		// 取得传入应用标识
		String appId = request.getParameter("appid");
		if (StringUtils.isEmpty(appId)) {
			throw new Exception("必须传入应用标识(appid)参数!");
		}

		request.setAttribute("appid", appId);

		return "make/appCompileIndex";
	}

}
